using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
using System.Globalization;

namespace PopLava.Tools
{
    public partial class BackgroundColorDialog : Form
    {
        private XmlDocument g_xmlDocument;
        private string g_sTileName = String.Empty;
        private string g_sCurrentViewID = String.Empty;
        private string g_sXmlType = "Current Tile";
        private string g_sType = String.Empty;
        
        // LeaderBoard == 103...

        public BackgroundColorDialog(string sTileName, ref XmlDocument xmlSavedSettings, string sCurrentViewID, string sType)
        {
            InitializeComponent();
            g_sTileName = sTileName;
            g_xmlDocument = xmlSavedSettings;
            g_sCurrentViewID = sCurrentViewID;
            g_sType = sType;
            this.Text = this.Text + " (" + sType + ")";
            this.BackgroundColor_PictureBox.BackColor = GetSavedColor();
        }
        private Color GetSavedColor()
        {
            Color cColor = Color.Transparent;
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
                        foreach (XmlNode n1 in xmlViews)
                        {
                            if (n1.FirstChild.InnerText == g_sCurrentViewID)
                            {
                                XmlNodeList xmlTiles = n1.SelectNodes("tile");
                                foreach (XmlNode n2 in xmlTiles)
                                {
                                    if (n2.FirstChild.InnerText == g_sTileName)
                                    {
                                        XmlNode n3 = null;
                                        if (g_sType == "static")
                                        {
                                            n3 = n2.SelectSingleNode("backgroundcolor");
                                            string sBackColor = n3.InnerText;
                                            if (sBackColor.Length > 0)
                                            {
                                                string[] arrBackColors = sBackColor.Split(',');
                                                cColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                                            }
                                        }
                                        else if (g_sType == "hover")
                                        {
                                            n3 = n2.SelectSingleNode("hoverbackgroundcolor");
                                            string sBackColor = n3.InnerText;
                                            if (sBackColor.Length > 0)
                                            {
                                                string[] arrBackColors = sBackColor.Split(',');
                                                cColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                                            }
                                        }
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                        break;
                    }
                case "Default For Current View":
                    {
                        XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
                        foreach (XmlNode n1 in xmlViews)
                        {
                            if (n1.FirstChild.InnerText == g_sCurrentViewID)
                            {
                                XmlNode n2 = null;
                                if (g_sType == "static")
                                {
                                    n2 = n1.SelectSingleNode("defaultbackgroundcolor");
                                    string sBackColor = n2.InnerText;
                                    if (sBackColor.Length > 0)
                                    {
                                        string[] arrBackColors = sBackColor.Split(',');
                                        cColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                                    }
                                }
                                else if (g_sType == "hover")
                                {
                                    n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
                                    string sBackColor = n2.InnerText;
                                    if (sBackColor.Length > 0)
                                    {
                                        string[] arrBackColors = sBackColor.Split(',');
                                        cColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                                    }
                                }
                                break;
                            }
                        }
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
                        XmlNode n2 = null;
                        if (g_sType == "static")
                        {
                            n2 = n1.SelectSingleNode("defaultbackgroundcolor");
                            string sBackColor = n2.InnerText;
                            if (sBackColor.Length > 0)
                            {
                                string[] arrBackColors = sBackColor.Split(',');
                                cColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                            }
                        }
                        else if (g_sType == "hover")
                        {
                            n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
                            string sBackColor = n2.InnerText;
                            if (sBackColor.Length > 0)
                            {
                                string[] arrBackColors = sBackColor.Split(',');
                                cColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                            }
                        }
                        break;
                    }
            }
            return cColor;
        }
        /// <summary>
        /// Saves the background color of a tile
        /// </summary>
        private void SaveTileBackgroundColor()
        {
            XmlHelper xmlHelper = new XmlHelper(ref g_xmlDocument);
            XmlNode n1 = xmlHelper.GetTileNode(g_sTileName, g_sCurrentViewID);
            if (n1 != null)
            {
                foreach (XmlNode n2 in n1)
                {
                    if (n2 == null || n2.Name.Length == 0)
                    {
                        MessageBox.Show("Fatal error occured while reading WinClickPro.xml (" + n1.Name + ")");
                        Environment.Exit(0);
                    }
                    XmlNode n3 = null;
                    if (g_sType == "static")
                    {
                        n3 = n1.SelectSingleNode("backgroundcolor");
                        n3.InnerText = GetAlphaChannel() + "," + BackgroundColor_PictureBox.BackColor.R.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.G.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.B.ToString(CultureInfo.InvariantCulture);
                        break;
                    }
                    else if (g_sType == "hover")
                    {
                        n3 = n1.SelectSingleNode("hoverbackgroundcolor");
                        n3.InnerText = GetAlphaChannel() + "," + BackgroundColor_PictureBox.BackColor.R.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.G.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.B.ToString(CultureInfo.InvariantCulture);
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Saves the background color of a tile
        /// </summary>
        private void SaveViewBackgroundColor()
        {
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n1 in xmlViews)
            {
                if (n1.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n2 = null;
                    if (g_sType == "static")
                    {
                        n2 = n1.SelectSingleNode("defaultbackgroundcolor");
                        n2.InnerText = GetAlphaChannel() + "," + BackgroundColor_PictureBox.BackColor.R.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.G.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.B.ToString(CultureInfo.InvariantCulture);
                        break;
                    }
                    else if (g_sType == "hover")
                    {
                        n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
                        n2.InnerText = GetAlphaChannel() + "," + BackgroundColor_PictureBox.BackColor.R.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.G.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.B.ToString(CultureInfo.InvariantCulture);
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Saves the background color of a tile
        /// </summary>
        private void SaveWinClickProBackgroundColor()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
            XmlNode n2 = null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultbackgroundcolor");
                n2.InnerText = GetAlphaChannel() + "," + BackgroundColor_PictureBox.BackColor.R.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.G.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.B.ToString(CultureInfo.InvariantCulture);
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
                n2.InnerText = GetAlphaChannel() + "," + BackgroundColor_PictureBox.BackColor.R.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.G.ToString(CultureInfo.InvariantCulture) + "," + BackgroundColor_PictureBox.BackColor.B.ToString(CultureInfo.InvariantCulture);
            }
        }
        /// <summary>
        /// Sets the backcolor to transparent
        /// </summary>
        private void BackgroundColorRemove_Button_Click(object sender, EventArgs e)
        {
            this.BackgroundColor_PictureBox.BackColor = Color.Transparent;
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        SaveTileBackgroundColor();
                        break;
                    }
                case "Default For Current View":
                    {
                        SaveViewBackgroundColor();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        SaveWinClickProBackgroundColor();
                        break;
                    }
            }
        }
        /// <summary>
        /// Opens a color picker and saves the users color choice
        /// </summary>
        private void BackgroundColorChange_Button_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.Color = this.BackgroundColor_PictureBox.BackColor;

            if (cd.ShowDialog() == DialogResult.OK)
            {
                this.BackgroundColor_PictureBox.BackColor = cd.Color;
            }
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        SaveTileBackgroundColor();
                        break;
                    }
                case "Default For Current View":
                    {
                        SaveViewBackgroundColor();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        SaveWinClickProBackgroundColor();
                        break;
                    }
            }
        }
        /// <summary>
        /// Returns 255 or 0 depending on the backcolor of the current tile
        /// </summary>
        private string GetAlphaChannel()
        {
            string sAlphaChannel = String.Empty;
            if (BackgroundColor_PictureBox.BackColor.Name == "Transparent")
            {
                sAlphaChannel = "0";
            }
            else
            {
                sAlphaChannel = "255";
            }
            return sAlphaChannel;
        }
        /// <summary>
        /// Restores the value to it's parent default.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackgroundColorRestore_Button_Click(object sender, EventArgs e)
        {
            switch (g_sXmlType)
            {
                case "Current Tile":
                    {
                        RestoreTileBackgroundColor();
                        SaveTileBackgroundColor();
                        break;
                    }
                case "Default For Current View":
                    {
                        RestoreViewBackgroundColor();
                        SaveViewBackgroundColor();
                        break;
                    }
                case "Default For WinClickPro (All)":
                    {
                        RestoreRootBackgroundColor();
                        SaveWinClickProBackgroundColor();
                        break;
                    }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreTileBackgroundColor()
        {
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n1 in xmlViews)
            {
                if (n1.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n2 = null;
                    if (g_sType == "static")
                    {
                        n2 = n1.SelectSingleNode("defaultbackgroundcolor");
                    }
                    else
                    {
                        n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
                    }
                    XmlNodeList xmlTiles = n1.SelectNodes("tile");
                    foreach (XmlNode n3 in xmlTiles)
                    {
                        if (n3.FirstChild.InnerText == g_sTileName)
                        {
                            XmlNode n4 = null;
                            if (g_sType == "static")
                            {
                                n4 = n3.SelectSingleNode("backgroundcolor");
                            }
                            else if (g_sType == "hover")
                            {
                                n4 = n3.SelectSingleNode("hoverbackgroundcolor");
                            }
                            n4.InnerText = n2.InnerText;
                            string sBackColor = n4.InnerText;
                            string[] arrBackColors = sBackColor.Split(',');
                            this.BackgroundColor_PictureBox.BackColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                            break;
                        }
                    }
                    break;
                }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreViewBackgroundColor()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement;
            XmlNode n2 = null;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultbackgroundcolor");
            }
            else
            {
                n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
            }
            XmlNodeList xmlViews = g_xmlDocument.DocumentElement.GetElementsByTagName("view");
            foreach (XmlNode n3 in xmlViews)
            {
                if (n3.FirstChild.InnerText == g_sCurrentViewID)
                {
                    XmlNode n4 = null;
                    if (g_sType == "static")
                    {
                        n4 = n3.SelectSingleNode("defaultbackgroundcolor");
                    }
                    else if (g_sType == "hover")
                    {
                        n4 = n3.SelectSingleNode("defaulthoverbackgroundcolor");
                    }
                    n4.InnerText = n2.InnerText;
                    string sBackColor = n4.InnerText;
                    string[] arrBackColors = sBackColor.Split(',');
                    this.BackgroundColor_PictureBox.BackColor = Color.FromArgb(Convert.ToInt32(arrBackColors[0], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[1], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[2], CultureInfo.CurrentCulture), Convert.ToInt32(arrBackColors[3], CultureInfo.CurrentCulture));
                    break;
                }
            }
        }
        /// <summary>
        /// Restores the tile to the view default
        /// </summary>
        private void RestoreRootBackgroundColor()
        {
            XmlNode n1 = g_xmlDocument.DocumentElement; // WinClickPro
            XmlNode n2;
            if (g_sType == "static")
            {
                n2 = n1.SelectSingleNode("defaultbackgroundcolor");
                n2.InnerText = "0,0,0,0";
                this.BackgroundColor_PictureBox.BackColor = Color.Transparent;
            }
            else if (g_sType == "hover")
            {
                n2 = n1.SelectSingleNode("defaulthoverbackgroundcolor");
                n2.InnerText = "0,0,0,0";
                this.BackgroundColor_PictureBox.BackColor = Color.Transparent;
            }
        }
        /// <summary>
        /// Set the type (tile, view, all)
        /// </summary>
        private void Tile_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Current Tile";
            this.BackgroundColor_PictureBox.BackColor = GetSavedColor();
        }
        /// Set the type (tile, view, all)
        private void View_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Default For Current View";
            this.BackgroundColor_PictureBox.BackColor = GetSavedColor();
        }
        /// Set the type (tile, view, all)
        private void WinClickPro_RadioButton_Click(object sender, EventArgs e)
        {
            g_sXmlType = "Default For WinClickPro (All)";
            this.BackgroundColor_PictureBox.BackColor = GetSavedColor();
        }
    }
}